<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSON-Encoding and Decoding Methods : Included Extensions : DataMapper ORM - User Guide</title>

<style type="text/css" media="all">@import url('../../css/userguide.css');</style>
<link rel="shortcut icon" type="image/png" href="../../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a> &nbsp;&#8250;&nbsp;
<a href="../../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
<a href="../extlist.html">Included Extensions</a> &nbsp;&#8250;&nbsp;
JSON-Encoding and Decoding Methods
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>JSON-Encoding and Decoding Methods (json)</h1>
<p class="note">To enable these methods, add <dfn>'json'</dfn> to DataMapper's config, under <strong>'extensions'</strong>.</p>
<p>Converts objects to and from a <a href="http://en.wikipedia.org/wiki/Json">JSON-encoded</a> string.</p>

<h2>to_json(<i>$fields</i>, <i>$pretty_print</i>)</h2>
<ul>
	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be included.  If empty or not provided, only the database columns will be included.</li>
	<li><dfn>$pretty_print</dfn>: (Optional) If <dfn>TRUE</dfn>, the JSON string will be formatted to be easier to read.  Defaults to <dfn>FALSE</dfn>.</li>
	<li><b>Returns</b>: A JSON-encoded view of the <var>$object</var>, or <dfn>FALSE</dfn> if an error occurs.</li>
</ul>
<p>Converts the <var>$object</var> into a JSON-encoded string.</p>

<h3>Usage</h3>
<pre>
<var>$u </var><kbd>= new </kbd><var>User</var><kbd>();
</kbd><var>$u</var><kbd>-&gt;</kbd><var><u>get_by_id</u></var><kbd>(</kbd><var>$user_id</var><kbd>);
</kbd><var>$u</var><kbd>-&gt;</kbd><var><s>set_json_content_type</s></var><kbd>();
echo </kbd><var>$u</var><kbd>-&gt;</kbd><var><s>to_json</s></var><kbd>(array(</kbd><dfn>'id'</dfn><kbd>, </kbd><dfn>'name'</dfn><kbd>, </kbd><dfn>'email'</dfn><kbd>), </kbd><var><dfn>TRUE</dfn></var><kbd>);</kbd>

<samp>// Outputs something like
// {"id": 1, "name":"Bob Johnson", "email":"bob.johnson@example.com"}</samp>
</pre>

<h2>all_to_json(<i>$fields</i>, <i>$pretty_print</i>)</h2>
<ul>
	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be included.  If empty or not provided, only the database columns will be included.</li>
	<li><dfn>$pretty_print</dfn>: (Optional) If <dfn>TRUE</dfn>, the JSON string will be formatted to be easier to read.  Defaults to <dfn>FALSE</dfn>.</li>
	<li><b>Returns</b>: A JSON-encoded view of <var>$object<kbd>-></kbd><i>all</i></var>, or <dfn>FALSE</dfn> if an error occurs.</li>
</ul>
<p>Converts <var>$object<kbd>-></kbd><i>all</i></var> into a JSON-encoded string.  The items will be placed into an array before conversion.</p>

<h2>from_json($json, <i>$fields</i>)</h2>
<ul>
	<li><dfn>$json</dfn>: The JSON-encoded string to save back to the object.</li>
	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be saved.  If empty or not provided, only the database columns will be saved.</li>
	<li><b>Returns</b>: <dfn>TRUE</dfn> on success, <dfn>FALSE</dfn> if the string conversion failed.</li>
</ul>
<p>Stores values encapsulated in a JSON-encoded string back on the <var>$object</var>.</p>

<h3>Usage</h3>
<pre>
<var>$json </var><kbd>= </kbd><var><b>$this</b></var><kbd>-&gt;</kbd><var>input</var><kbd>-&gt;</kbd><var>post</var><kbd>(</kbd><dfn>'notedata'</dfn><kbd>);
</kbd><var>$n </var><kbd>= new </kbd><var>Note</var><kbd>();
if(</kbd><var>$n</var><kbd>-&gt;</kbd><var><s>from_json</s></var><kbd>(</kbd><var>$json</var><kbd>))
{
    </kbd><samp>// TODO: verify input, save changes
</samp><kbd>}
else
{
    </kbd><var>show_error</var><kbd>(</kbd><dfn>'Invalid input'</dfn><kbd>);
}</kbd>
</pre>

<h2>set_json_content_type()</h2>
<p>A simple method used to set the proper content type header (<b>&ldquo;Content-Type: application/json&rdquo;</b>) for JSON files.

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:&nbsp;&nbsp;<a href=""></a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;</span>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../../index.html">User Guide Home</a>
<span id="footer_next">&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="../license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../../js/mootools.js"></script>
<script type="text/javascript" src="../../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../../',
			pagespath: '../',
			last: 'extlist'
		});

	});
//-->
</script>
</body>
</html>
